package com.udaos.modules.train.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.udaos.common.base.Result;
import com.udaos.common.filter.PrivateFilter;
import com.udaos.common.page.DataTableColumn;
import com.udaos.common.page.DataTableOrder;
import com.udaos.models.sys.Sys_user;
import com.udaos.models.train.Ud_trainlump_product;
import com.udaos.modules.company.services.UdCompanyService;
import com.udaos.modules.project.services.UdProjectService;
import com.udaos.modules.region.services.UdRegionService;
import com.udaos.modules.train.services.UdTrainlumpProductService;


/**
 * @author mutaotao
 * @time 2017-03-07 15:15:46
 * 
 */
@IocBean
@At("/platform/train/lumpproduct")
@Filters({ @By(type = PrivateFilter.class)})
public class UdTrainlumpProductController {
	@Inject
	private UdTrainlumpProductService udTrainlumpProductService;
	@Inject
	private UdRegionService regionService;
	@Inject
	private UdCompanyService companyService;
	@Inject
	private UdProjectService projectService;

	/**
	 * 首页
	 */
	@At("")
	@Ok("beetl:/platform/base/train/lumpproduct/index.html")
	@RequiresAuthentication
	public void index() {
		
	}
	/**
	 * 加载数据
	 * @param length
	 * @param start
	 * @param draw
	 * @param order
	 * @param columns
	 * @return
	 */
	@At
	@Ok("json:full")
	@RequiresAuthentication
	public Object data(@Param("name") String name,@Param("length") int length, @Param("start") int start, @Param("draw") int draw, @Param("::order") List<DataTableOrder> order, @Param("::columns") List<DataTableColumn> columns) {
		return udTrainlumpProductService.init(length, start, draw,name);
	}
	
	/**
	 * TO 添加
	 */
	@At
	@Ok("beetl:/platform/base/train/lumpproduct/add.html")
	@RequiresAuthentication
	public Object add() {
		Map<String,Object> map=new HashMap<String,Object>();
		//获取开通区域
		map.put("regions", udTrainlumpProductService.getRegions());
		//获取项目
		map.put("projects",projectService.find());
		return map;
	}
	/**
	 * DO 添加
	 * @param ud_trainlump_product
	 * @param req
	 * @return
	 */
	@At
	@Ok("json")
	public Object addDo(@Param("..") Ud_trainlump_product ud_trainlump_product, HttpServletRequest req) {
		try {
			//获取主体
			Subject subject = SecurityUtils.getSubject();
			Sys_user user = (Sys_user) subject.getPrincipal();
			String company_id=user.getUnit().getCompany_id();
			ud_trainlump_product.setCompany_id(company_id);
			udTrainlumpProductService.insert(ud_trainlump_product);
			return Result.success("保存成功");
		} catch (Exception e) {
			return Result.error("保存失败");
		}
	}
	
	@At("/edit/?")
	@Ok("beetl:/platform/base/train/lumpproduct/edit.html")
	@RequiresAuthentication
	public Object edit(String id) {
		Map<String,Object> map=new HashMap<String,Object>();
		//获取开通区域
		map.put("regions", udTrainlumpProductService.getRegions());
		//获取项目
		map.put("projects",projectService.find());
		map.put("product", udTrainlumpProductService.fetch(id));
		return map;
	}
	
	@At
	@Ok("json")
	public Object editDo(@Param("..") Ud_trainlump_product ud_trainlump_product, HttpServletRequest req) {
		try {
			udTrainlumpProductService.updateIgnoreNull(ud_trainlump_product);
			return Result.success("保存成功");
		} catch (Exception e) {
			return Result.error("保存失败");
		}
	}
	/**
	 * 删除
	 * @param id
	 * @param req
	 * @return
	 */
	@At("/delete/?")
	@Ok("json")
	public Object delete(String id, HttpServletRequest req) {
		try {
			udTrainlumpProductService.delete(id);
			return Result.success("删除成功");
		} catch (Exception e) {
			return Result.error("删除失败");
		}
	}
	
	

}